<template>
  <div class="new-book">
    <div class="new-book-main">

      <div class="new-book-t">
        <div class="title-hot">
          <div class="hot">NEW</div>
          <div class="hot-c"></div>
        </div>
        <div class="title-txt">新上好书</div>
      </div>
      <!-- 新上好书数据展示 -->
      <ul class="new-book-b">
        <li
       
          v-for="(item) in newBookPaginationList" 
          :key="item.book_id" 
          @click="gotoDetails(item.book_id)"

        >
          <img :src="item.photo || default_src">
          <div class='book-content'>
						<h3>{{ item.book_name }}</h3>
						<div class="author">{{ item.author }}</div>
            <div class="publisher">{{ item.publisher }}</div>
						<div class='price'>
							<div class='price-vip'>新书推荐</div>
							<div class='price-total'>¥ {{ item.price }} </div>
						</div> 
					</div>
          <span v-show="!item.statuss" class="notListed">未上架</span>
        </li>
      </ul>

      <!-- 分页 -->
      <!-- 每页8条数据 -->
      <div class="pagination-one">
        <el-pagination 
          background 
          layout="prev, pager, next" 
          :total="newBookList.length" 
          :page-size="8"
          @current-change="newCurrentChange"
        />
      </div>
      
    </div>

    <div class="new-book-main">
      
      <div class="new-book-t-2">
        <div class="title-txt">推荐好书</div>
        <div class="title-hot">
          <div class="hot">HOT</div>
          <div class="hot-c"></div>
        </div>
      </div>

      <!-- 推荐好书数据展示 -->
      <ul class="new-book-b">
        <li
          v-for="(item) in recBookPaginationList" 
          :key="item.book_id"
          @click="gotoDetails(item.book_id)" 
        >
          <img :src="item.photo || default_src">
          <div class='book-content'>
						<h3>{{ item.book_name }}</h3>
						<div class="author">{{ item.author }}</div>
            <div class="publisher">{{ item.publisher }}</div>
						<div class='price'>
							<div class='price-vip'>热门好书</div>
							<div class='price-total'>¥ {{ item.price }} </div>
						</div> 
					</div>
          <span v-show="!item.statuss" class="notListed">未上架</span>
        </li>
      </ul>
      <!-- 分页 -->
      <div class="pagination-two">
        <el-pagination 
          background 
          layout="prev, pager, next" 
          :total="recBookList.length" 
          :page-size="pageSize"
          @current-change="recCurrentChange"
        />
      </div>

    </div>


  </div>
</template>

<script setup>
// api
import bookModelApi from '@/request/api/book.js'
import { reactive, toRefs, watchEffect } from 'vue';

// utils
import { getRandomItems } from '@/utils/arrayUtil.js'

// router
import { useRouter } from 'vue-router';

// store
import { useStore } from 'vuex'
const store = useStore();

const router = useRouter();

// 初始化所有书籍列表
let bookList = ref([]);

// 计算属性，用于计算并返回拆分后的前半段数组
// 新上好书数据
const newBookList = computed(() => {
  return bookList.value.slice(0, Math.ceil(bookList.value.length / 2));
});

// 计算属性，用于计算并返回拆分后的后半段数组
// 推荐好书数据
const recBookList = computed(() => {
  return bookList.value.slice(Math.ceil(bookList.value.length / 2));
});


// 轮播图底部推荐
let swiperBottomList = ref([]);

// 初始化请求接口
watchEffect(async () => {
  const res = await bookModelApi.getAllBookApi();

  if(res.code != 200) return;
  bookList.value = res.data;

  // 将所有书籍存入vuex中
  store.commit("setAllBookList", bookList.value);

  // 新上好书和推荐好书
  const { length } = bookList.value;
  newBookList.value = bookList.value.slice(0, Math.ceil(length / 2));
  recBookList.value = bookList.value.slice(Math.ceil(length / 2));

  if(bookList.value.length != 0){
    // 轮播图下面的5个推荐
    swiperBottomList.value = getRandomItems(bookList.value,5);
    // 存入vuex 方便Swiper组件使用
    store.commit('setSwiperBottomList',swiperBottomList.value);
  }
  
})

// 默认图书封面(占位)
const default_src = 'src/assets/default.jpg';

// 当前页码
let newCurrentPage = ref(1);
let recCurrentPage = ref(1);
// 每页条数
const pageSize = 8;

// 分页8条数据
const newBookPaginationList = computed(() => {
  return newBookList.value.slice((newCurrentPage.value - 1) * pageSize, newCurrentPage.value * pageSize);
})

const recBookPaginationList = computed(() => {
  return recBookList.value.slice((recCurrentPage.value - 1) * pageSize, recCurrentPage.value * pageSize);
})

// 新上好书页码改变
const newCurrentChange = ( page ) => {
  newCurrentPage.value = page;
}

// 推荐好书页码改变
const recCurrentChange = ( page ) => {
  recCurrentPage.value = page;
}

// 点击去详情页
const gotoDetails = ( id ) => {
  router.push({
    path: `/details`,
    // 显式传参
    query: { 
      book_id: id
    }
  })
}


</script>

<style lang='less' scoped>
.new-book{
  padding: 38px 0;
  background: #f3f5f9;
}
.new-book-main{
  width: 1000px;
  margin: 0 auto;
}
.new-book-t{
  display: flex;
}
.new-book-t-2{
  display: flex;
  // border: 1px solid red;
  flex-direction: row-reverse;
  margin-top: 30px;
}
.title-hot{
  display: flex;
}
.title-hot .hot{
	width: 65px;
	line-height: 32.5px;
	text-align: center;
	color: #fff;
	background: linear-gradient(90deg, #FF727F 0%, #FC685C 100%);
	border-radius: 5px 0px 5px 5px;
}
.title-hot .hot-c{
  width: 0;
  height: 0;
  border:3px solid #fc685c;
  border-right-color: transparent;
  border-bottom-color: transparent;
}
.title-txt{
  margin-left: 11px;
  position: relative;
	z-index: 999;
	font-size: 23px;
  line-height: 35px;
	color: #333333;
}
.title-txt::after{
  content:'';
  position: absolute;
  z-index: -1;
  left: 0;
	bottom: 0;
  width: 120%;
	margin-left: -10%;
  height: 9px;
	background: linear-gradient(90deg, #FBF84F 0%, #FEA935 100%);
	border-radius: 5px;
}

.new-book-b{
  display: flex;
  flex-wrap: wrap;
  margin-top: 38px;
  height: 780px;
  box-shadow: rgba(50, 50, 93, 0.25) 0px 50px 100px -20px, rgba(0, 0, 0, 0.3) 0px 30px 60px -30px, rgba(10, 37, 64, 0.35) 0px -2px 6px 0px inset;
}
.new-book-b li{
  width: 240px;
	height: 350px;
  margin-top: 38px;
  margin-right: 10px;
	background: #FFFFFF;
	box-shadow: 1px 3px 3px rgba(27, 39, 94, 0.1);
	border-radius: 5px;
  transition: .45s;
  position: relative;
}
.new-book-b li:hover{
  cursor: pointer;
  transform: translateY(-30px);
}
.new-book-b li img{
  width: 240px;
  height: 220px;
}
.new-book-b .book-content{
  padding: 7px 16px;
  .author{
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis; /* 文字溢出省略号 */
    white-space: nowrap; /* 不换行 */
  }
}
.new-book-b .book-content h3{
  font-size: 12px;
	color: #333333;
}
.book-content > div{
  padding: 7px 0;
  font-size: 12px;
	color: #808080;
}
.price{
  display: flex;
}
.price-vip{
  background: linear-gradient(90deg, #FF928E 0%, #FE7062 99%);
	border-radius: 16px 0px 16px 0px;
  text-align: center;
  color:#fff;
  width: 64px;
	height: 16px;
  margin-right: 3px;
}
.price-total{
  font-size: 12px;
	color: #FF727F;
}

.notListed{
  border:1px solid #000;
  padding: 5px;
  position: absolute;
  font-size: 35px;
  left:50%;
  top:50%;
  transform: translate(-50%,-50%) rotate(-30deg);
  color: #fff;
  background: rgba(29,29,31,0.72);
  backdrop-filter: saturate(180%) blur(20px);
}

.new-book-main {
  .pagination-one{
    display: flex;
    align-items: center;
    margin-top: 10px;
    justify-content: flex-end;
  }
  .pagination-two{
    display: flex;
    align-items: center;
    margin-top: 10px;
    justify-content: center;
  }
 
}
</style>